package com.hzhc.controller.gate;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.VideoPointInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.VideoType;
import com.hzhc.service.GateConfigService;
import com.hzhc.service.VideoPointInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.GateConfigVo;
import com.hzhc.vo.GateVideoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 卡口设备配置
 *
 * @author 功不唐捐
 * @since 2024-06-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/gateConfig")
public class GateConfigController extends BaseController {

    private final GateConfigService gateConfigService;


    private final VideoPointInfoService videoPointInfoService;

    /**
     * 列表
     *
     * @param vo
     * @return
     */
    @PostMapping("/getList")
    public ResultUtil<List<GateConfigVo>> getDictTypeList(@RequestBody GateConfigVo vo) {
        Page<GateConfigVo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<GateConfigVo> types = gateConfigService.getList(page, vo);
        return ResultUtil.build(types,page.getTotal(), page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 添加
     *
     * @param vo
     * @param request
     * @return
     */
    @PostMapping("/addRet")
    @SysTemLog(operModul = "卡口设备配置", operType = "新增", operDesc = "卡口设备配置-添加", modular = "PC")
    public ResultUtil<T> addRet(HttpServletRequest request, @RequestBody GateConfigVo vo) {
        UserDto user = TokenUtil.getUser(request);
        vo.setUserId(user.getId().intValue());
        boolean b = gateConfigService.addRet(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }


    /**
     * 修改
     */
    @PostMapping("/updateRet")
    @SysTemLog(operModul = "卡口设备配置", operType = "编辑", operDesc = "卡口设备配置-编辑", modular = "PC")
    public ResultUtil<T> updateRet(HttpServletRequest request, @RequestBody GateConfigVo vo) {
        boolean b = gateConfigService.updateRet(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    /**
     * 删除
     */
    @PostMapping("/deleteRet")
    @SysTemLog(operModul = "卡口设备配置", operType = "删除", operDesc = "卡口设备配置-删除", modular = "PC")
    public ResultUtil<T> deleteRet(HttpServletRequest request, @RequestBody GateConfigVo vo) {
        boolean b = gateConfigService.deleteRet(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }


    /**
     * 卡口监控
     */
    @ResponseBody
    @RequestMapping("/getGateVideo")
    public ResultUtil<GateVideoVo> getGateVideo(@RequestBody String json) {
        List<VideoPointInfo> list = videoPointInfoService.list(Wrappers.<VideoPointInfo>lambdaQuery()
                .eq(VideoPointInfo::getDisable, false)
                .in(VideoPointInfo::getType, VideoType.KAOKOU)
                .orderByAsc(VideoPointInfo::getSort)
        );
        JSONObject obj = JSON.parseObject(json);
        GateVideoVo vo = new GateVideoVo();
        vo.setJk1(list.get(0));
        vo.setCk1(list.get(1));
        vo.setJk2(list.get(2));
        vo.setCk2(list.get(3));
        vo.setType(obj.getString("type"));
        return ResultUtil.build(vo, ResultUtilCodeEnum.SUCCESS);
    }
}
